const mongoose = require('mongoose')
mongoose.Promise= Promise

//链接数据库
mongoose.connect('mongodb://localhost/User')

var db = mongoose.connection
db.on('open',function(){
	console.log('数据库已经连接...')
})
db.on('error',function(){
	console.log('数据库连接失败...')
})

//建立模式
var Schema = mongoose.Schema

var UserSchema = new Schema({
	username:String,
	image:String,
	password:String,
	repassword:String,
	isMale:Boolean,
    emaile:String,
    course:String,
    time:Date,
})

//模型   mongoose.model 第一个参数是集合的名称 这里最好写上复数形式
//第二个参数是自定义模式
var User = mongoose.model('users',UserSchema)

var AskSchema = new Schema({
	name:String,
	time:Date,
	content:String,
	ans:Array,
	userId:{type:Schema.Types.ObjectId,ref:'users'}
})

var Ask = mongoose.model('asks',AskSchema)

var AnswerSchema = new Schema({
	name:String,
	content:String,
	time:Date,
    userId:{type:Schema.Types.ObjectId,ref:'users'},
	askId:{type:Schema.Types.ObjectId,ref:'asks'}
})

var Answer = mongoose.model('answers',AnswerSchema)

module.exports = {
	User,
    Ask,
    Answer
}
